package com.j283_5.wisdom_park.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.mapper.*;
import com.j283_5.wisdom_park.pojo.*;
import com.j283_5.wisdom_park.service.PropertyPayCostService;
import com.j283_5.wisdom_park.vo.property_pay_cost.PropertyPayCostVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class PropertyPayCostServiceImpl implements PropertyPayCostService {
    @Resource
    private PropertyPayCostMapper propertyPayCostMapper;
    @Resource
    private PersonMapper personMapper;
    @Resource
    private LoginMapper loginMapper;
    @Resource
    private AdminMapper adminMapper;

    @Override
    public ResultData tenantPropertyList(Page<PropertyPayCost> page) {
        QueryWrapper<PropertyPayCost> propertyPayCostQueryWrapper = new QueryWrapper<>();
        propertyPayCostQueryWrapper.eq("ppc_state",0).eq("ppc_worth_state",0);
        Page<PropertyPayCost> payCostPage = propertyPayCostMapper.selectPage(page, propertyPayCostQueryWrapper);
        List<PropertyPayCost> recordList = payCostPage.getRecords();
        if (recordList != null){
            HashMap<String, Object> map = new HashMap<>();
            map.put("count",payCostPage.getTotal());
            ArrayList<PropertyPayCostVo> payCostVoArrayList = new ArrayList<>();
            for(PropertyPayCost propertyPayCost : recordList){
                PropertyPayCostVo propertyPayCostVo = new PropertyPayCostVo();
                BeanUtils.copyProperties(propertyPayCost,propertyPayCostVo);
                Person person = personMapper.selectById(propertyPayCost.getPersonId());
                propertyPayCostVo.setPersonName(person.getPersonName());
                payCostVoArrayList.add(propertyPayCostVo);
            }
            if(payCostVoArrayList.size() > 0){
                map.put("data",payCostVoArrayList);
                return ResultData.ok().data(map);
            }
        }
        return ResultData.error().message("查询失败!");
    }

    @Override
    public ResultData WorthOf(ArrayList<Integer> ppcIdList) {
        UpdateWrapper<PropertyPayCost> propertyPayCostUpdateWrapper = new UpdateWrapper<>();
        propertyPayCostUpdateWrapper.in("ppc_id",ppcIdList);
        propertyPayCostUpdateWrapper.set("ppc_worth_state",1);
        int i = propertyPayCostMapper.update(null, propertyPayCostUpdateWrapper);
        if(i>0){
            return ResultData.ok().message("催缴成功");
        }else{
            return ResultData.error().message("催缴失败");
        }
    }

    @Override
    public ResultData peopleTenant(Integer logId) {
        QueryWrapper<Person> personQueryWrapper = new QueryWrapper<>();
        personQueryWrapper.eq("log_id",logId);
        Person person = personMapper.selectOne(personQueryWrapper);
        if(person != null){
            QueryWrapper<PropertyPayCost> propertyPayCostQueryWrapper = new QueryWrapper<>();
            propertyPayCostQueryWrapper.eq("person_id",person.getPersonId());
            propertyPayCostQueryWrapper.eq("ppc_worth_state",1);
            propertyPayCostQueryWrapper.eq("ppc_state",0);
            List<PropertyPayCost> propertyPayCosts = propertyPayCostMapper.selectList(propertyPayCostQueryWrapper);
            if(propertyPayCosts.size() > 0){
                return ResultData.ok().data(propertyPayCosts);
            }
            return ResultData.error().message("没有需要支付的账单");
        }
        return ResultData.error().message("账号信息异常");
    }

    @Override
    public ResultData pay(Integer logId, Integer ppcType) {
        QueryWrapper<Person> personQueryWrapper = new QueryWrapper<>();
        personQueryWrapper.eq("log_id",logId);
        Person person = personMapper.selectOne(personQueryWrapper);

        if(person != null){
            UpdateWrapper<PropertyPayCost> propertyPayCostUpdateWrapper = new UpdateWrapper<>();
            propertyPayCostUpdateWrapper.eq("person_id",person.getPersonId());
            propertyPayCostUpdateWrapper.eq("ppc_state",0);
            propertyPayCostUpdateWrapper.eq("ppc_type",ppcType);
            propertyPayCostUpdateWrapper.set("ppc_real_date",System.currentTimeMillis());
            propertyPayCostUpdateWrapper.set("ppc_state",1);
            int i = propertyPayCostMapper.update(null, propertyPayCostUpdateWrapper);
            if(i > 0 ){
                return ResultData.ok().message("支付成功!");
            }else{
                return ResultData.ok().message("支付失败!");
            }
        }
        return ResultData.error().message("没有查询到用户信息");
    }
}
